﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Windows.Threading;
using System.Threading;

namespace wine {

    class ImagePlayer {
        private Timer parentTimer;
        delegate void UpdateTimer();
        private int intervalTime = 50;

        private MainWindow _windowUI;

        private string _filePath;
        private string _prefix;
        private int _startNum;
        private int _imageCount;
        private int currentImageNum;

        private Image imagePlayerElement;
        public ImagePlayer(MainWindow window,Image imageElement, string filePath, string prefix, int startNum, int imageCount, int interval = 50) {
            this._windowUI = window;
            this._prefix = prefix;
            this._filePath = filePath;
            this._startNum = startNum;
            this._imageCount = imageCount;
            this.currentImageNum = startNum;
            intervalTime = interval;

            imagePlayerElement = imageElement;

        }
        ~ImagePlayer() {
            if (this.parentTimer != null) {
                parentTimer.Dispose();
            }
        }

        public void Play() {
            SetUpTimer();
        }
        private void SetUpTimer() {
            parentTimer = new Timer(new TimerCallback(OnTimedEvent));
            //每秒执行一次
            parentTimer.Change(0, intervalTime);
        }

        private void OnTimedEvent(object state) {
            _windowUI.Dispatcher.BeginInvoke(new UpdateTimer(Update));
        }

        private void Update() {
            try {
                Uri uri = new Uri(@"pack://siteoforigin:,,,/" + this._filePath + "/" + this._prefix + PadLeft(currentImageNum) + ".png", UriKind.RelativeOrAbsolute);

                imagePlayerElement.Source = new BitmapImage(uri);
                currentImageNum++;
            } catch (Exception e) {

            }
            //reset
            if (currentImageNum >= _imageCount + _startNum) {
                currentImageNum = _startNum;
            }
        }
        public void Stop() {
            parentTimer.Dispose();
        }

        private string PadLeft(int num) {

            string result = "";

            if (num <= 9) {
                result = "0000" + num;
            } else if (num > 9 && num <= 99) {
                result = "000" + num;
            } else if (num > 99 && num <= 999) {
                result = "00" + num;
            }
            return result;


        }
    }
}
